package ChessBoard;
import java.util.ArrayList;


public class ChessBoard {
	public ArrayList<Figure> figures = new ArrayList<Figure>();
	King whiteKing;
	King blackKing;
	Pawn whitePawn1;
	Pawn whitePawn2;
	Pawn whitePawn3;
	Pawn whitePawn4;
	Pawn whitePawn5;
	Pawn whitePawn6;
	Pawn whitePawn7;
	Pawn whitePawn8;
	Pawn blackPawn1;
	Pawn blackPawn2;
	Pawn blackPawn3;
	Pawn blackPawn4;
	Pawn blackPawn5;
	Pawn blackPawn6;
	Pawn blackPawn7;
	Pawn blackPawn8;
	Rook whiteRook1;
	Rook whiteRook2;
	Rook blackRook1;
	Rook blackRook2;
	Knight whiteKnight1;
	Knight whiteKnignt2;
	Knight blackKnight1;
	Knight blackKnight2;
	Bishop whiteBishop1;
	Bishop whiteBishop2;
	Bishop blackBishop1;
	Bishop blackBishop2;
	Queen whiteQueen;
	Queen blackQueen;

	public ChessBoard() {
		whiteKing = new King(true, new int[] { 5, 1 }, this);
		figures.add(whiteKing);
		blackKing = new King(false, new int[] { 5, 8 }, this);
		figures.add(blackKing);
		whitePawn1 = new Pawn(true, new int[] { 1, 2 }, this);
		figures.add(whitePawn1);
		whitePawn2 = new Pawn(true, new int[] { 2, 2 }, this);
		figures.add(whitePawn2);
		whitePawn3 = new Pawn(true, new int[] { 3, 2 }, this);
		figures.add(whitePawn3);
		whitePawn4 = new Pawn(true, new int[] { 4, 2 }, this);
		figures.add(whitePawn4);
		whitePawn5 = new Pawn(true, new int[] { 5, 2 }, this);
		figures.add(whitePawn5);
		whitePawn6 = new Pawn(true, new int[] { 6, 2 }, this);
		figures.add(whitePawn6);
		whitePawn7 = new Pawn(true, new int[] { 7, 2 }, this);
		figures.add(whitePawn7);
		whitePawn8 = new Pawn(true, new int[] { 8, 2 }, this);
		figures.add(whitePawn8);
		blackPawn1 = new Pawn(false, new int[] { 1, 7 }, this);
		figures.add(blackPawn1);
		blackPawn2 = new Pawn(false, new int[] { 2, 7 }, this);
		figures.add(blackPawn2);
		blackPawn3 = new Pawn(false, new int[] { 3, 7 }, this);
		figures.add(blackPawn3);
		blackPawn4 = new Pawn(false, new int[] { 4, 7 }, this);
		figures.add(blackPawn4);
		blackPawn5 = new Pawn(false, new int[] { 5, 7 }, this);
		figures.add(blackPawn5);
		blackPawn6 = new Pawn(false, new int[] { 6, 7 }, this);
		figures.add(blackPawn6);
		blackPawn7 = new Pawn(false, new int[] { 7, 7 }, this);
		figures.add(blackPawn7);
		blackPawn8 = new Pawn(false, new int[] { 8, 7 }, this);
		figures.add(blackPawn8);
		whiteRook1 = new Rook(true, new int[] { 1, 1 }, this);
		figures.add(whiteRook1);
		whiteRook2 = new Rook(true, new int[] { 8, 1 }, this);
		figures.add(whiteRook2);
		blackRook1 = new Rook(false, new int[] { 1, 8 }, this);
		figures.add(blackRook1);
		blackRook2 = new Rook(false, new int[] { 8, 8 }, this);
		figures.add(blackRook2);
		whiteKnight1 = new Knight(true, new int[] { 2, 1 }, this);
		figures.add(whiteKnight1);
		whiteKnignt2 = new Knight(true, new int[] { 7, 1 }, this);
		figures.add(whiteKnignt2);
		blackKnight1 = new Knight(false, new int[] { 2, 8 }, this);
		figures.add(blackKnight1);
		blackKnight2 = new Knight(false, new int[] { 7, 8 }, this);
		figures.add(blackKnight2);
		whiteBishop1 = new Bishop(true, new int[] { 3, 1 }, this);
		figures.add(whiteBishop1);
		whiteBishop2 = new Bishop(true, new int[] { 6, 1 }, this);
		figures.add(whiteBishop2);
		blackBishop1 = new Bishop(false, new int[] { 3, 8 }, this);
		figures.add(blackBishop1);
		blackBishop2 = new Bishop(false, new int[] { 6, 8 }, this);
		figures.add(blackBishop2);
		whiteQueen = new Queen(true, new int[] { 4, 1 }, this);
		figures.add(whiteQueen);
		blackQueen = new Queen(false, new int[] { 4, 8 }, this);
		figures.add(blackQueen);
	}

	public ChessBoard(boolean isEmpty) {

	}

	public boolean fieldIsEmpty(int[] position) {
		for (Figure a : figures) {
			if (a.getPosition()[0] == position[0]
					&& a.getPosition()[1] == position[1]) {
				return false;
			}
		}
		return true;
	}

	public Figure getFigure(int[] pos) {
		for (Figure f : figures) {
			if (pos[0] == f.position[0] && pos[1] == f.position[1]) {
				return f;
			}
		}
		return null;
	}

	public boolean[] checkCheck() {
		boolean[] checks = new boolean[] { false, false }; // checks[0] is
															// whiteUnderCheck,
															// vice versa
		Figure whiteKingTMP = null;
		Figure blackKingTMP = null;
		for (Figure f : figures) {
			if (f instanceof King) {
				if (f.isWhite) {
					whiteKingTMP = f;
				} else {
					blackKingTMP = f;
				}
			}
		}
		for (Figure f : figures) {
			f.checkCheck();
			if (f.isWhite && f.checks) {
				checks[1] = true;
			} else if (!f.isWhite && f.checks) {
				checks[0] = true;
			}
		}

		return checks;
	}

	public void refreshAllFigures() {
		for (Figure f : figures) {
			f.calculatePossibleMove();
		}
		for (Figure f : figures) {
			calculateCoveredBy(f);
		}
	}

	protected void calculateCoveredBy(Figure fig) {
		for (Figure f : figures) {
			if (f.covers.contains(fig)) {
				fig.coveredBy++;
			}
		}
	}

	public Figure copyFigure(Figure f, ChessBoard board) {
		Figure tmp;
		if (f instanceof King) {
			tmp = new King(f.isWhite, f.position, board);
		} else if (f instanceof Bishop) {
			tmp = new Bishop(f.isWhite, f.position, board);
		} else if (f instanceof Knight) {
			tmp = new Knight(f.isWhite, f.position, board);
		} else if (f instanceof Queen) {
			tmp = new Queen(f.isWhite, f.position, board);
		} else if (f instanceof Pawn) {
			tmp = new Pawn(f.isWhite, f.position, board);
		} else {
			tmp = new Rook(f.isWhite, f.position, board);
		}
		return tmp;
	}

	protected boolean moveProvokesNoCheck(int x, int y, Figure f) {
//		int tmp = 0;
//		if (!f.isWhite) {
//			tmp = 1;
//		}
//		ChessBoard curFig;
//		curFig = copyCurPosition();
//		Figure f2 = curFig
//				.getFigure(new int[] { f.position[0], f.position[1] });
//		curFig.deleteFigure(f);
//		f2.setPosition(new int[] { x, y });
//		curFig.figures.add(f);
//		return !curFig.checkCheck()[tmp];
		return true;
	}

	public void refreshAllChecks(ArrayList<Figure> curFig) {
		for (Figure f : curFig) {
			f.checkCheck();
		}
	}

	public ChessBoard copyCurPosition() {
		ChessBoard tmp = new ChessBoard(true);
		tmp.figures = new ArrayList<Figure>();
		for (Figure f : figures) {
			Figure tmp2 = this.copyFigure(f, tmp);
			tmp.figures.add(tmp2);
		}
		return tmp;

	}

	public void deleteFigure(Figure f) {
		this.figures.remove(f);
	}

	public Figure getKing(boolean isWhite) {
		for (Figure f : figures) {
			if (f instanceof King && f.isWhite == isWhite)
				return f;
		}

		return null;
	}
}
